package GUILayer;

import java.awt.EventQueue;
import java.util.List;

import CTRLayer.PersonCTR;
import CTRLayer.ResponseListCTR;
import CTRLayer.TrainedCTR;

import ModelLayer.Person;
import ModelLayer.Trained;
import CTRLayer.TrainingDayCTR;
import GUIAdapter.PanelTableModelAdapter;
import GUIAdapter.TrainingModel;
import ModelLayer.TrainingDay;
import ModelLayer.ResponseList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class Training extends JFrame
{
	private PersonCTR pc;
	private TrainingDayCTR tdc;
	private TrainedCTR tctr;
	private Person child;
	private Person trainer;
	private TrainingDay trainingDay;
	private JTextField lblChild;
	private JTextField lblTrainer;
	private JTextField lblDate;
	private JTextField lblTime;
	private JComboBox comboBox;
	private ResponseListCTR responseListCTR;
	private List<ResponseList> responseListList;
	private PanelTableModelAdapter trainingModel;
	private int indexTrainingModel;
	private JFrame frame;
	private JPanel contentPane;

	/**
	 * Create the frame.
	 */
	public Training()
	{
		frame = new JFrame();
		frame.setTitle("ParaManagement");
		frame.setBounds(100, 100, 1000, 600);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		pc = new PersonCTR();
		tdc = new TrainingDayCTR();
		tctr = new TrainedCTR();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1000, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JButton btnLogbog = new JButton("Logbog");
		btnLogbog.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				new Logbook();
			}
		});
		
		JButton btnAktivitetsdetaljer = new JButton("Aktivitetsdetaljer");
		btnAktivitetsdetaljer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new ActivityDetails();
			}
		});
		
		JButton btnUdfrTrning = new JButton("Udfoer Traening");
		btnUdfrTrning.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//SwingUtilities.updateComponentTreeUI(frame);
			}
		});
		
		JButton btnForrigeAktivitet = new JButton("Forrige Aktivitet");
		
		JButton btnNsteAktivitet = new JButton("Naeste Aktivitet");
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		
		comboBox = new JComboBox();
		
		JLabel lblVlgAktivitet = new JLabel("Vaelg Aktivitet:");
		
		responseListCTR = new ResponseListCTR();
		
		responseListList = responseListCTR.findAllResponseLists();

		trainingModel = new PanelTableModelAdapter(new TrainingModel(responseListList));
		
		trainingModel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		trainingModel.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		trainingModel.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{
			@Override
			public void valueChanged(ListSelectionEvent event)
			{
				if (event.getValueIsAdjusting() == false)
				{
					indexTrainingModel = trainingModel.getTable().getSelectedRow();
				}
			}
		});
		
		GroupLayout gl_contentPane = new GroupLayout(frame.getContentPane());
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(trainingModel, GroupLayout.DEFAULT_SIZE, 1164, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 387, GroupLayout.PREFERRED_SIZE)
							.addGap(76)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 481, Short.MAX_VALUE)
									.addComponent(btnAktivitetsdetaljer)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(btnLogbog))
								.addComponent(lblVlgAktivitet)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnForrigeAktivitet)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnNsteAktivitet)
							.addPreferredGap(ComponentPlacement.RELATED, 825, Short.MAX_VALUE)
							.addComponent(btnUdfrTrning)))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblVlgAktivitet)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(btnLogbog)
								.addComponent(btnAktivitetsdetaljer)
								.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(trainingModel, GroupLayout.PREFERRED_SIZE, 300, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 133, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnForrigeAktivitet)
						.addComponent(btnNsteAktivitet)
						.addComponent(btnUdfrTrning))
					.addContainerGap())
		);
		
		JLabel lblTrner = new JLabel("Traener:");
		
		JLabel lblBarn = new JLabel("Barn:");
		
		JLabel lblTidspunkt = new JLabel("Tidspunkt:");
		
		JLabel lblDato = new JLabel("Dato:");
		
		lblChild = new JTextField("");
		lblChild.setBorder(null);
		lblChild.setEditable(false);
		lblChild.setColumns(10);
		
		lblTrainer = new JTextField("");
		lblTrainer.setBorder(null);
		lblTrainer.setEditable(false);
		lblTrainer.setColumns(10);
		
		lblTime = new JTextField("");
		lblTime.setBorder(null);
		lblTime.setEditable(false);
		lblTime.setColumns(10);
		
		lblDate = new JTextField("");
		lblDate.setBorder(null);
		lblDate.setEditable(false);
		lblDate.setColumns(10);
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblBarn)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblChild, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblTrner)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblTrainer, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblTidspunkt)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblTime, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblDato)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblDate)))
					.addContainerGap(29, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBarn)
						.addComponent(lblChild, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblTidspunkt)
						.addComponent(lblTime, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTrner)
						.addComponent(lblTrainer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblDato)
						.addComponent(lblDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(32))
		);
		panel.setLayout(gl_panel);
		//contentPane.setLayout(gl_contentPane);
		frame.getContentPane().setLayout(gl_contentPane);
		
		getChild();
		getTrainer();
		getDate();
		getTime();
		getAllTrained();
	}
	
	public void getChild()
	{
		String phonenr = "98211345";
		child = pc.findPersonPhoneNr(phonenr);
		
		if(child == null)
        {
        	lblChild.setText("Barnet eksisterer ikke.");
        }
        else
        {
        	lblChild.setText(child.getfName() + " " + child.getlName());
        }
    }
	
	public void getTrainer()
	{
		String phonenr = "98211347";
		trainer = pc.findPersonPhoneNr(phonenr);
		
		if(trainer == null)
        {
        	lblTrainer.setText("Traener eksisterer ikke.");
        }
        else
        {
        	lblTrainer.setText(trainer.getfName() + " " + trainer.getlName());
        }
	}
	
	public void getDate()
	{
		trainingDay = tdc.findTrainingDayDate("2012-05-30");
		lblDate.setText(trainingDay.getDate());
	}
	
	public void getTime()
	{
		trainingDay = tdc.findTrainingDayID(1);
		SimpleDateFormat time = new SimpleDateFormat("HH:mm");

		//Indsaet tid string der hvor quotesene er
		Date d;
		try {
			d = time.parse(trainingDay.getTrainingStart());
			String start = time.format(d);
			d = time.parse(trainingDay.getTrainingEnd());
			String slut = time.format(d);
			lblTime.setText(start + " - " +slut );
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void getAllTrained()
	{
		int id = 1;
		trainingDay = tdc.findTrainingDayID(id);
		ArrayList<Trained> trlist = tctr.findAllTrainedByTrainingDayID(trainingDay.getTrainingDayID());
		int index = 0;
		if(trlist == null)
		{
			System.out.println("Der er ingen aktiviteter!");
		}
		else
		{
			while(index < trlist.size())
			{
				comboBox.addItem(trlist.get(index).getName());
				index++;
			}
		}
	}
}
